 function [mag1 mag2 modeBack] = myRt(mag,mode,tol)
% RECIBE
% mag: magnitud a analizar
% mode: si es capacitor (valores < 1) o resistencia (valores > 1)
% tol: toleracia de los valores nominales(1% o 5%). Capacitores usa las
% mismas tablas
% RETORNA
% mag1:la primer magnitud
% mag2:la segunda magnitud
% modeback: 0 serie. 1 paralelo.
    table = defineTable(mode,tol);
    [mag1 mag2 modeBack] = findTheBestValues(mag,mode,table);
end

function [table] = defineTable (mode,tol)
    if((mode == 'r')&&(tol==5))
        table = [0 1 1.20 1.5 1.8 2.2 2.7 3.3 3.9 4.7 5.6 6.8 8.2];
    else if((mode == 'r')&&(tol==1))
            table=[0 1 1.02 1.05 1.07 1.1 1.13 1.15 1.18 1.21 1.24 1.27 1.3 1.33 1.37 1.4 1.43 1.47 1.5 1.54 1.58 1.62 1.65 1.69 1.74 1.78 1.82 1.87 1.91 1.96 ...       
            2 2.05 2.1 2.15 2.21 2.26 2.32 2.37 2.43 2.49 2.55 2.61 2.67 2.74 2.8 2.87 2.94 3.01 3.09 3.16 3.24 3.32 3.4 3.48 3.57 3.65 3.74 3.83 3.92 ...
            4.02 4.12 4.22 4.32 4.42 4.53 4.64 4.75 4.87 4.99 5.11 5.23 5.36 5.49 5.62 5.76 5.9 6.04 6.19 6.34 6.49 6.65 6.81 6.988 7.15 7.32 7.5 7.68 7.87...
            8.06 8.25 8.45 8.66 8.87 9.09 9.31 9.53 9.76];
        else if(mode == 'c')
                table=[0 1 1.20 1.5 1.8 2.2 2.7 3.3 3.9 4.7 5.6 6.8 8.2];
             end
        end
    end     
end
function [r1 r2 modeBack diff] = findValues(mag,mode,table1,table2)
    diff = mag;
    r1=0;
    r2=0;
    modeBack=0;
    for  i =1 : length(table1)
        for j =1 : length(table2)
            mymagseries= series(table1(i),table2(j));            
            if((table1(i)==0) && (table2(j)==0)) 
                mymagparalel=0;
            else
                mymagparalel=paralel(table1(i),table2(j));
            end
            
            if((abs(mag-mymagseries)<diff)||(abs(mag-mymagparalel)<diff))
                r1 = table1(i);
                r2=table2(j);
                if ((abs(mag-mymagseries))<(abs(mag-mymagparalel))) 
                    modeBack= 0;
                    diff=abs(mag-mymagseries); 
                else
                    modeBack=1;
                    diff=abs(mag-mymagparalel);
                end
            end
        end
    end
    
    if (mode=='c')
        modeBack = ~modeBack;
    end
    
end

function [r1 r2 modeBack] = findTheBestValues(mag,mode,table)
    rResultados=[];
    diffs=[];
    if(mode == 'r') 
        for i= -1 : 8
            for j = -1 : 8
                [r1 r2 modeBack diff] = findValues(mag,mode,table*(10^(i)),table*(10^(j)));
                rResultados(length(rResultados)+1)=r1;
                rResultados(length(rResultados)+1)=r2;
                rResultados(length(rResultados)+1)=modeBack;
                diffs(length(diffs)+1)=diff;
            end
        end
        
    else
        for i= -12 : -3
            for j = -12 : -3
                [r1 r2 modeBack diff] = findValues(mag,mode,table*(10^(i)),table*(10^(j)));
                rResultados(length(rResultados)+1)=r1;
                rResultados(length(rResultados)+1)=r2;
                rResultados(length(rResultados)+1)=modeBack;
                diffs(length(diffs)+1)=diff;
            end
        end
    end

    d=min(diffs);
    i=find(diffs==d,1,'first');
    r1=rResultados(i+2*(i-1));
    r2=rResultados(i+2*(i-1)+1);
    modeBack=rResultados(i+2*(i-1)+2);
    
end


function mymagseries= series(num1,num2)
    mymagseries= num1+num2;
end

function mymagparalel=paralel(num1,num2)
    mymagparalel=(num1*num2)/(num1+num2);
end

